package com.qf.zyh.dao.impl;

import com.qf.zyh.dao.EmpDao;
import com.qf.zyh.entity.Emp;
import com.qf.zyh.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class EmpDaoImpl implements EmpDao {
    private QueryRunner qr = new QueryRunner();
    @Override
    public List<Emp> selectAll() {
        try {
            return qr.query(DBUtil.getConn(),"select eno,ename,esex,salary,pwd,dno,date from emp",new BeanListHandler<>(Emp.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Emp selectin(String name) {
        try {
            return qr.query(DBUtil.getConn(),"select eno,ename,esex,salary,pwd,dno,date  from emp where ename=?",new BeanHandler<>(Emp.class),name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int insert(Emp emp) {
        try {
            return qr.update(DBUtil.getConn(),"insert into emp values (default,?,?,?,?,?,?)",
                    emp.getEname(),emp.getEsex(),emp.getSalary(),emp.getPwd(),emp.getDno(),emp.getDate());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(Integer i) {
        try {
            return qr.update(DBUtil.getConn(),"delete from emp where eno=?",i);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(Emp emp) {
        try {
            return qr.update(DBUtil.getConn(),"update emp set ename=?,esex=?,salary=?,pwd=?,dno=?,date=? where eno=?"
                    ,emp.getEname(),emp.getEsex(),emp.getSalary(),emp.getPwd(),emp.getDno(),emp.getDate(),emp.getEno());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Emp selectid(int id) {
        try {
            return qr.query(DBUtil.getConn(),"select eno,ename,esex,salary,pwd,dno,date from emp where eno=?",new BeanHandler<>(Emp.class),id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Emp> selectPage(Integer startRow, Integer pageSize) {
        try {
            return qr.query(DBUtil.getConn(),"select eno,ename,esex,salary,pwd,dno,date from emp limit ?,?",new BeanListHandler<>(Emp.class),startRow,pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Long getCount() {
        try {
            return qr.query(DBUtil.getConn(),"select count(eno) from emp",new ScalarHandler<>());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
